package com.whj.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.whj.domain.dto.AddArticleDto;
import com.whj.domain.dto.CategoryDTO;
import com.whj.domain.entity.Category;
import com.whj.domain.entity.Link;
import com.whj.domain.enums.AppHttpCodeEnum;
import com.whj.domain.response.ResponseResult;
import com.whj.domain.vo.ArticleDetailVo;
import com.whj.domain.vo.CategoryVO;
import com.whj.domain.vo.ExcelCategoryVo;
import com.whj.domain.vo.PageVo;
import com.whj.service.ArticleService;
import com.whj.service.CategoryService;
import com.whj.service.LinkService;
import com.whj.utils.BeanCopyUtils;
import com.whj.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Auther: wanghaijun
 * @Date: 2023/6/6 - 06 - 06 - 9:10 
 * @Description: com.whj.controller
 */

@RestController
@RequestMapping("/content")
public class ContentController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private ArticleService articleService;

    /**
     * 查询所有分类的接口
     *
     * @return
     */
    @GetMapping("/category/listAllCategory")
    public ResponseResult listAllCategory() {
        List<CategoryVO> list = categoryService.listAllCategory();
        return ResponseResult.okResult(list);
    }

    /**
     * 导出excel
     *
     * @param response
     */
    @PreAuthorize("@ps.hasPermission('content:category:export')")
    @GetMapping("/category/export")
    public void export(HttpServletResponse response) {
        try {
            //设置下载文件的请求头
            WebUtils.setDownLoadHeader("分类.xlsx", response);
            //获取需要导出的数据
            List<Category> categoryVos = categoryService.list();

            List<ExcelCategoryVo> excelCategoryVos = BeanCopyUtils.copyBeanList(categoryVos, ExcelCategoryVo.class);
            //把数据写入到Excel中
            EasyExcel.write(response.getOutputStream(), ExcelCategoryVo.class)
                    .autoCloseStream(Boolean.FALSE)
                    .sheet("分类导出")
                    .doWrite(excelCategoryVos);

        } catch (Exception e) {
            //如果出现异常也要响应json
            ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
            WebUtils.renderString(response, JSON.toJSONString(result));
        }
    }

    @GetMapping("/article/list")
    public ResponseResult<PageVo> list(Integer pageNum, Integer pageSize, String title, String summary) {

        return articleService.selectByTitleAndSummary(pageNum, pageSize, title, summary);
    }

    /**
     * 查询文章详细信息
     *
     * @return
     */
    @GetMapping("/article/{id}")
    public ResponseResult<ArticleDetailVo> articleDetail(@PathVariable("id") Long id) {
        return articleService.articleDetail(id);
    }

    /**
     * 更新文章详细
     *
     * @param addArticleDto
     * @return
     */
    @PutMapping("/article")
    public ResponseResult updateArticle(@RequestBody AddArticleDto addArticleDto) {
        return articleService.updateArticle(addArticleDto);
    }

    /**
     * 删除文章 逻辑删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/article/{id}")
    public ResponseResult deleteArticle(@PathVariable("id") List<String> id) {

        System.out.println("=================");
        return articleService.deleteArticle(id);
    }

    @GetMapping("/category/list")
    public ResponseResult selectCategory(Integer pageNum, Integer pageSize, String name, String status) {
        return categoryService.selectCategoryByPage(pageNum, pageSize, name, status);
    }

    /**
     * 新增分类
     *
     * @param categoryVO
     * @return
     */
    @PostMapping("/category")
    public ResponseResult addCategory(@RequestBody CategoryVO categoryVO) {
        return categoryService.addCategory(categoryVO);
    }

    /**
     * 查询分类名
     *
     * @param id
     * @return
     */
    @GetMapping("/category/{id}")
    public ResponseResult selecCategoryById(@PathVariable("id") Long id) {

        return categoryService.selecCategoryById(id);
    }

    /**
     * 更新分类
     *
     * @param categoryDTO
     * @return
     */
    @PutMapping("/category")
    public ResponseResult updateCategory(@RequestBody CategoryDTO categoryDTO) {
        return categoryService.updateCategory(categoryDTO);
    }

    @DeleteMapping("/category/{id}")
    public ResponseResult deleteCategory(@PathVariable("id") List<String> id) {
        return categoryService.deleteCategory(id);
    }

    @Autowired
    private LinkService linkService;

    /**
     *  分页查询友链列表
     *
     * @param pageNum
     * @param pageSize
     * @param name
     * @param status
     * @return
     */
    @GetMapping("/link/list")
    public ResponseResult selectLinkByPage(Integer pageNum, Integer pageSize, String name, String status){
        return linkService.selectLinkByPage(pageNum, pageSize, name, status);
    }

    /**
     * 增加友链
     *
     * @param link
     * @return
     */
    @PostMapping("/link")
    public ResponseResult addLink(@RequestBody Link link){
        return linkService.addLink(link);
    }

    @GetMapping("/link/{id}")
    public ResponseResult selectLinkById(@PathVariable("id") Long id){
        return linkService.selectLinkById(id);
    }

    /**
     * 修改友链
     *
     * @param link
     * @return
     */
    @PutMapping("/link")
    public ResponseResult updateLink(@RequestBody Link link){
        return linkService.updateLink(link);
    }

    /**
     * 删除友链
     *
     * @param id
     * @return
     */
    @DeleteMapping("/link/{id}")
    public ResponseResult delete(@PathVariable("id") List<String> id){
        return linkService.delete(id);
    }

    @PutMapping("/link/changeLinkStatus")
    public ResponseResult changeStatus(@RequestBody Link link){

        return linkService.changeStatus(link.getId(),link.getStatus());
    }
}
